package com.dcbeida.activity.source_unit;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.bean.SourceList;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.SourceModel;

import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hp on 2016/5/12.
 */
public class SourcePresenter implements SourceContract.Present {
    private final SourceModel model;
    private SourceContract.View view;
    private Activity act;

    public SourcePresenter(SourceContract.View view, Activity act) {
        this.view = view;
        model = SourceModel.newInstance();
        this.act = act;
    }

    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };

    @Override
    public void getSourceList() {
        model.getSourceList(new SourcePresenter.GetSourceListCallBack(act, SourceList.class, lis));
    }

    @Override
    public void getSourcelInfo(String keyword) {
        model.getSourcelInfo(keyword, new SourcePresenter.GetHospitalInfoCallBack(act, SourceList.class, lis));
    }

    @Override
    public void start() {

    }

    class GetSourceListCallBack<T> extends DialogCallback<T> {

        public GetSourceListCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (((SourceList) data).getList().size() >= 0) {
                    view.showList(((SourceList) data).getList());
                }
            } else {
                view.showTelTip("没有查询到来货单位");
            }
        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTelTip("数据未请求成功,请检查网络...");


        }
    }

    class GetHospitalInfoCallBack<T> extends DialogCallback<T> {

        public GetHospitalInfoCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (((SourceList) data).getList().size() >= 0) {
                    view.showList(((SourceList) data).getList());
                }
            } else {
                view.showTelTip("没有查询到来货单位");
            }
        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTelTip("数据未请求成功,请检查网络...");

        }
    }
}
